//
//  TeacherListView.m
//  text1
//
//  Created by edz on 2017/10/31.
//  Copyright © 2017年 edz. All rights reserved.
//

#import "TeacherListView.h"
#import "TeacherListCellTableViewCell.h"

@interface TeacherListView () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) NSMutableArray *addArr;
@property (strong, nonatomic) NSMutableArray *addDataArr;
@property (strong, nonatomic) NSMutableArray *teacherArr;


@end

@implementation TeacherListView


- (instancetype)initWithFrame:(CGRect)frame {
    
    
    if (self = [super initWithFrame:frame]) {
      
    }
    
    return self;
    
    
}



-(void)awakeFromNib {
    
    [super awakeFromNib];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

-(void)setLessonId:(NSNumber *)lessonId{
    
    _lessonId = lessonId;
    
    [self getNet:[lessonId stringValue]];

}

//- (void) getSureNet {
//
//    //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=saveTeachers&uids=126287,126288&lessonId=721
//
//    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=saveTeachers&lessonId=%@",[self.lessonId stringValue]];
//    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
//        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
//
//        }
//    } Fail:^(NSError *Error) {
//
//    }];
//}




-(void) getNet:(NSString *)lessonId {
    NSLog(@"%@",self.lessonId);
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=getLessonTeachserList&lessonId=%@",[self.lessonId stringValue]];
    NSLog(@"%@  %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            
            NSArray *t = [Obj valueForKey:@"t"];
            
            for (NSDictionary *dict in t) {
                
                [self.dataArr addObject:dict];
            }
            [self.listTableView reloadData];
            
        }
        
    } Fail:^(NSError *Error) {
        
        [SVProgressHUD showErrorWithStatus:NetErr];
        
    }];
}

#pragma mark - 懒加载
-(NSMutableArray *)addArr {
    if (_addArr ==nil) {
        _addArr = [[NSMutableArray alloc]init];
    }
    return  _addArr;
}

-(NSMutableArray *)teacherArr {
    if (_teacherArr == nil) {
        _teacherArr = [[NSMutableArray alloc]init];
    }
    return  _teacherArr;
}

- (NSMutableArray *)dataArr {
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
        
    }
    return _dataArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TeacherListCellTableViewCell *cell = [TeacherListCellTableViewCell initWithTableView:tableView];
    
    cell.teacherNameLabel.text = [NSString stringWithFormat:@"%@",self.dataArr[indexPath.row]];
    if([self.addArr containsObject:indexPath]){
        cell.selectImage.image =[UIImage imageNamed:@"btn_choice_seleted"];
    }else {
        cell.selectImage.image = [UIImage imageNamed:@"btn_choice_default"];
    }
    NSDictionary *dict =  _dataArr[indexPath.row];
    
    cell.teacherNameLabel.text = dict[@"petName"];
    

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
    
    TeacherListCellTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if([self.addArr containsObject:indexPath]){
        
        [self.addArr removeObject:indexPath];
        
        
        cell.selectImage.image = [UIImage imageNamed:@"btn_choice_default"];
        
    }else {
        
        [self.addArr addObject:indexPath];
        
        cell.selectImage.image =[UIImage imageNamed:@"btn_choice_seleted"];
        
    }
    
    

    
    
}




- (IBAction)didSureBtn:(UIButton *)sender {

    if (_delegate &&[_delegate respondsToSelector:@selector(didAddTeacherButtonWithArray:)]) {
        [_delegate didAddTeacherButtonWithArray:_addArr];
    }
//    [_delegate didAddTeacherButtonWithArray:_addArr];
    for (NSIndexPath *path in self.addArr) {
        NSDictionary *dict =  _dataArr[path.row];
        [self.teacherArr addObject:dict[@"uid"]];
        
    }
//    NSLog(@"!!~~%@",self.teacherArr);
    NSString *string = [self.teacherArr componentsJoinedByString:@","];
//    NSLog(@"%@",string);
    //action=saveTeachers
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=saveTeachers&lessonId=%@&uids=%@",[self.lessonId stringValue],string];
    NSLog(@"%@",url);
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [_teacherArr removeAllObjects];
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            [SVProgressHUD showSuccessWithStatus:@"添加成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.lastreload?self.lastreload():nil;
                [self removeFromSuperview];
            });
        } else {
            
            [SVProgressHUD showErrorWithStatus:Obj[@"msg"]];
            
        }

    } Fail:^(NSError *Error) {
        [_teacherArr removeAllObjects];

        [SVProgressHUD showErrorWithStatus:NetErr];

    }];
   
    
    
}






@end
